package com.kuaimacode.kframework.agent.services.category;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.mybatis.mapper.agent.SysAgentCategoryCfgMapper;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentCategoryCfg;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 分站物品品类设置 服务类
 * </p>
 *
 * @author Bean
 * @since 2019-03-17
 */
@Service
public class CategoryService extends ServiceImpl<SysAgentCategoryCfgMapper, SysAgentCategoryCfg> {


    /**
     * 查询分站未添加的分类。
     * @param type
     * @return
     */
    public List<SysAgentCategoryCfg> queryListByType(String type){
        return baseMapper.queryAgentNotAddCategory(SessionUtil.getAgentId(),type);

    }


    /**
     *  分页查找所有的品类
     * @param pageNum 当前页码，从1开始
     * @param pageSize  一页显示多少行
     * @return
     */
    public PageInfo<SysAgentCategoryCfg> findAll(int pageNum, int pageSize,SysAgentCategoryCfg categoryCfg) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("agent_id", SessionUtil.getAgentId());
        Optional.ofNullable(categoryCfg.getType()).ifPresent(type->queryWrapper.eq("type",type));
        Optional.ofNullable(categoryCfg.getCategory()).ifPresent(category->queryWrapper.like("category",category));
        List<SysAgentCategoryCfg> users = baseMapper.selectList(queryWrapper);
        return new PageInfo<SysAgentCategoryCfg>(users);
    }

    /**
     * 物品品类恢复平台默认设置
     */
    @Transactional
    public void init(){
        //删除现有的品类配置
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("agent_id",SessionUtil.getAgentId());
        baseMapper.delete(queryWrapper);
        //插入平台默认品类信息
        baseMapper.init(SessionUtil.getAgentId(),SessionUtil.getUserId());

    }



	
}
